<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Carousel as Carousels;
use think\Db;

class Carousel extends Common {

    private $cModel;   //当前控制器关联模型

    public function _initialize() {
        parent::_initialize();
        $this->cModel = new Carousels;   //别名：避免与控制名冲突
    }

    /*
     * 轮播图列表
     * @param mixed $sort 排序
     * @author cpy(1442435897@qq.com)
     * @return array
     */
    public function index() {
        $sort = 'sort asc,update_time desc,create_time desc';
        $dataList = $this->cModel->selectInfo('', $sort);
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    /*
     * 轮播图列表
     * @param mixed $param 传参
     * @author cpy(1442435897@qq.com)
     * @return array
     */
    public function create() {
        $param = request()->param();
        if($param){
            try{
                $param['create_time'] = time();
                $param['update_time'] = time();
                $judge = $this->judgeSort($param['sort']);
                if(!empty($judge)){
                    $row = $this->cModel->update(['sort'=>'13'],['id' => $judge['id']]);
                }else{
                    $row = 1;
                }
                $res = $this->cModel->allowField(true)->insert($param);
                if($res && $row){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }
        return $this->fetch();
    }

    /*
     * 轮播图列表
     * @param mixed $param 传参
     * @author cpy(1442435897@qq.com)
     * @return array
     */
    public function edit() {
        $param = request()->param();
        if($_POST){
            try{
                $judge = $this->judgeSort($param['sort']);
                if(empty($judge)){
                    $row = 1;
                }else{
                    $row = $this->cModel->allowField(true)->save(['sort'  => '13'],['id' => $judge['id']]);
                }
                $res = $this->cModel->allowField(true)->save($param,['id' => $param['id']]);
                if($res && $row){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('Carousel/index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }
        $where['id'] = ['eq',$param['id']];
        $data =  $this->cModel->findInfo($where,'*');
        $this->assign('data', $data);
        return $this->fetch();
    }
    
    /*
     * 判断位置是否有图
     * @param mixed $sort 位置
     * @author cpy(1442435897@qq.com)
     * @return array
     */
    public function judgeSort($sort) {
        $where['sort'] = ['eq',$sort];
        $res =  $this->cModel->findInfo($where,'*');
        return $res;
    }

    public function delete() {
        $param = request()->param();
        $row = $this->cModel->allowField(true)->save(['delete_time'  => time()],['id' => $param['id']]);
        if($row){
            return ajaxReturn(lang('action_success'), url('Carousel/index'));
        }else{
            return ajaxReturn($this->cModel->getError());
        }
    }

}
